function Tests = modelParserTest()
Tests = functiontests(localfunctions);
end
%#ok<*DEFNU>


%**************************************************************************


function testQuotes(This)

m = model('testQuotes.model');

% Descriptions of variables.
actDescript = get(m, 'description');
expDescript = struct( ...
    'x', 'Variable x', ...
    'y', 'Variable y', ...
    'z', 'Variable z' ...
    );
verifyEqual(This, actDescript, expDescript);

% Equation labels.
actLabel = get(m,'label');
expLabel = { ...
    'Equation x', ...
    'Equation y', ...
    'Equation z', ...
    };
verifyEqual(This, actLabel, expLabel);

end % testQuotes()


%**************************************************************************


function testForControlInQuotes(This)

m = model('testForControlInQuotes.model');
expLabel = { ...
    'Equation for X', ...
    'Equation for Y', ...
    'Equation for Z', ...
    };
actLabel = get(m,'labels');
assertEqual(This,actLabel,expLabel);

end % testForControlInQuotes()


%**************************************************************************


function testBracketsInQuotes(This)

m = model('testBracketsInQuotes.model');

% Descriptions of variables.
actDescript = get(m,'description');
expDescript = struct( ...
    'x','Variable x ([%])', ...
    'y','(Variable y)', ...
    'z','Variable z' ...
    );
verifyEqual(This,actDescript,expDescript);

% Equation labels.
actLabel = get(m,'label');
expLabel = { ...
    '[Equation x]((', ...
    '{Equation {y', ...
    'Equation} z}', ...
    };
verifyEqual(This,actLabel,expLabel);

end % testBracketsInQuotes()


%**************************************************************************


function testAssignments(This)

m = model('testAssignment.model');

% Values assigned to variables in model file.
actAssign = get(m,'sstate');
expAssign = struct( ...
    'x',(1 + 2) + 1i, ...
    'y',complex(3*normpdf(2,1,0.5),2), ...
    'z',[1,2,3]*[4,5,6]' ...
    );
verifyEqual(This,actAssign,expAssign);

end % testAssignments()


%**************************************************************************


function testMultipleAssignments(This)

m = model('testMultipleAssignment.model');

% Values assigned to variables in model file.
actAssign = get(m,'sstate');
expAssign = struct( ...
    'x',[1,2,3], ...
    'y',[4,5,6], ...
    'z',[1,1,1], ...
    'w',[NaN,NaN,NaN], ...
    'alp',[10,10,10], ...
    'bet',sin([1,2,3]) ...
    );
assertEqual(This,actAssign,expAssign);

end % testMutlipleAssignments()


%**************************************************************************


function testAutoexogenize(This)

m = model('testAutoexogenize.model');

% Values assigned to variables in model file.
actAssign = get(m,'autoexogenize');
expAssign = struct( ...
    'x','ex', ...
    'y','ey', ...
    'z','ez', ...
    'w','ew' ...
    );
assertEqual(This,actAssign,expAssign);

end % testAutoexogenize()


%**************************************************************************


function testEvalTimeSubs(This)

p = theparser();

eqtn = { ...
    'x{-1+1} - y{0} + z{-4} + x{10+10}', ...
    'x{0} + y{-0} + z{-0+1-1}', ...
    'x{0} + y{+-5} + z{+1}', ...
    'x{t-1} + y{t+4-4} + z{t+10}', ...
    'x{0+}', ...
    };
eqtn = strrep(eqtn,' ','');
[actEqtn,actMaxT,actMinT,actValidTimeSubs] = evaltimesubs(p,eqtn);

expEqtn = { ...
    'x - y + z{@-4} + x{@+20}', ...
    'x + y + z', ...
    'x + y{@-5} + z{@+1}', ...
    'x{@-1} + y + z{@+10}', ...
    'x', ...
};
expEqtn = strrep(expEqtn,' ','');
expMaxT = 20;
expMinT = -5;
expValidTimeSubs = [true,true,true,true,false];

assertEqual(This,actEqtn,expEqtn);
assertEqual(This,actMaxT,expMaxT);
assertEqual(This,actMinT,expMinT);
assertEqual(This,actValidTimeSubs,expValidTimeSubs);

end % testEvalTimeSubs()


%**************************************************************************


function testParseEqtns(This)

p = theparser();

blk = [ ...
    '; ; ;', ...
    'x =# x{-1+1-1+1-1} + y{-1} + z{0} !! x=x+y+z;', ...
    'x := y{5+5} + z !! ;', ...
    'x{+0} += y{-0} + z{0} !! x{+0} =# y{+-3} + z{0};', ...
    '; ; ; ', ...
    'x + y + z;', ...
    ];

[actEqtn,actEqtnLabel,actEqtnLhs,actEqtnRhs,actEqtnSign, ...
    actSstateLhs,actSstateRhs,actSstateSign, ...
    actMaxT,actMinT] = parseeqtns(p,blk);

expEqtn = { ...
    'x =# x{-1+1-1+1-1} + y{-1} + z{0} !! x=x+y+z;', ...
    'x := y{5+5} + z !! ;', ...
    'x{+0} += y{-0} + z{0} !! x{+0} =# y{+-3} + z{0};', ...
    'x+y+z;', ...
    };
expEqtn = strrep(expEqtn,' ','');

expEqtnLabel = {'','','',''};

expEqtnLhs = { ...
    'x', ...
    'x', ...
    'x', ...
    '', ...
    };

expEqtnRhs = { ...
    'x{@-1}+y{@-1}+z', ...
    'y{@+10}+z', ...
    'y+z', ...
    'x+y+z', ...
    };

expEqtnSign = { ...
    '=#', ...
    ':=', ...
    '+=', ...
    '', ...
    };

expSstateLhs = { ...
    'x', ...
    '', ...
    'x', ...
    '', ...
    };

expSstateRhs = { ...
    'x+y+z', ...
    '', ...
    'y{@-3}+z', ...
    '', ...
    };

expSstateSign = { ...
    '=', ...
    '', ...
    '=#', ...
    '', ...
    };

expMaxT = 10;
expMinT = -3;

assertEqual(This,actEqtn,expEqtn);
assertEqual(This,actEqtnLabel,expEqtnLabel);
assertEqual(This,actEqtnLhs,expEqtnLhs);
assertEqual(This,actEqtnRhs,expEqtnRhs);
assertEqual(This,actEqtnSign,expEqtnSign);
assertEqual(This,actSstateLhs,expSstateLhs);
assertEqual(This,actSstateRhs,expSstateRhs);
assertEqual(This,actSstateSign,expSstateSign);
assertEqual(This,[actMaxT,actMinT],[expMaxT,expMinT]);

end % testParseEqtns()
